package licode.unisop.single.user.infrastructure.repository.dao;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import licode.unisop.provider.info.SiGroupAssignToUsersIn;
import licode.unisop.provider.utils.StrUtil;
import licode.unisop.single.user.application.entity.UserEntity;
import licode.unisop.single.user.application.gateway.UserDao;
import licode.unisop.single.user.infrastructure.assembler.UniUserSingleAssembler;
import licode.unisop.single.user.infrastructure.repository.mapper.UserMapper;
import licode.unisop.single.user.infrastructure.repository.po.UserPO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * uni-user-single
 *
 * @author licode
 * @date 2016-08-27 15:28:34
 */
@Component
public class UserDaoImpl
        extends ServiceImpl<UserMapper, UserPO>
        implements UserDao {
    @Resource
    private UniUserSingleAssembler uniUserSingleAssembler;

    @Override
    public void add(UserEntity item) {
        super.save(uniUserSingleAssembler.fromUserEntity(item));
    }

    @Override
    public void update(UserEntity item) {
        super.updateById(uniUserSingleAssembler.fromUserEntity(item));
    }

    @Override
    public void removeWithId(Serializable id) {
        super.removeById(id);
    }

    @Override
    public UserEntity getWithId(Serializable id) {
        return uniUserSingleAssembler.fromUser(super.getById(id));
    }

    @Override
    public void updateBatch(List<UserEntity> items) {
        super.updateBatchById(uniUserSingleAssembler.fromUserEntity(items));
    }

    @Override
    public List<UserEntity> findAssignUsers(SiGroupAssignToUsersIn req) {
        if (null == req.getUserIds() || req.getUserIds().isEmpty()) {
            return new ArrayList<>();
        } else {
            return uniUserSingleAssembler.fromUser(super.lambdaQuery()
                    .eq(UserPO::getRealmId, req.getRealmId())
                    .in(UserPO::getUserId, req.getUserIds())
                    .eq(StrUtil.isNotBlank(req.getGroupId()), UserPO::getGroupId, req.getGroupId())
                    .list());
        }
    }
}
